package com.nanhai.buddhist.college.module.collegebase.dal.mysql.classes;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.datapermission.core.annotation.DataPermission;
import com.nanhai.buddhist.college.framework.mybatis.core.mapper.BaseMapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesInfoDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.CollegeAndClassesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.colleges.CollegesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.departments.DepartmentsDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionsDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 学校管理-班级 Mapper
 *
 * @author 开发
 */
@Mapper
public interface ClassesMapper extends BaseMapperX<ClassesDO> {

    default PageResult<ClassesDO> selectPage(ClassesPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ClassesDO>()
                .eqIfPresent(ClassesDO::getClassesNumber, reqVO.getClassesNumber())
                .likeIfPresent(ClassesDO::getClassesName, reqVO.getClassesName())
                .eqIfPresent(ClassesDO::getClassesCode, reqVO.getClassesCode())
                .eqIfPresent(ClassesDO::getClassesGrade, reqVO.getClassesGrade())
                .betweenIfPresent(ClassesDO::getOpeningTime, reqVO.getOpeningTime())
                .orderByDesc(ClassesDO::getCreateTime));
    }

    default PageResult<ClassesDO> selectPage0(ClassesPageReqVO reqVO) {
        return selectPageForJoin(reqVO, ClassesDO.class, new MPJLambdaWrapperX<ClassesDO>()
                .eqIfPresent(ClassesDO::getClassesNumber, reqVO.getClassesNumber())
                .likeIfPresent(ClassesDO::getClassesName, reqVO.getClassesName())
                .eqIfPresent(ClassesDO::getClassesCode, reqVO.getClassesCode())
                .eqIfPresent(ClassesDO::getClassesGrade, reqVO.getClassesGrade())
                .geIfPresent(ClassesDO::getOpeningTime,reqVO.getOpeningStartDate())
                .leIfPresent(ClassesDO::getOpeningTime,reqVO.getOpeningEndTimeDate())
                .eqIfPresent(ClassesDO::getProfessionId, reqVO.getProfessionsId())
                .eqIfPresent(DepartmentsDO::getId, reqVO.getDepartmentsId())
                .eqIfPresent(CollegesDO::getId, reqVO.getCollegeId())
                .betweenIfPresent(ClassesDO::getOpeningTime, reqVO.getOpeningTime())
                .leftJoin(ProfessionsDO.class, ProfessionsDO::getId, ClassesDO::getProfessionId)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getId, ProfessionsDO::getDepartmentsId)
                .leftJoin(CollegesDO.class, CollegesDO::getId, DepartmentsDO::getCollegeId)
                .selectAll(ClassesDO.class)
                .selectAs(ProfessionsDO::getProfessionName, ClassesDO::getProfessionsName)
                .selectAs(DepartmentsDO::getDepartmentName, ClassesDO::getDepartmentsName)
                .selectAs(CollegesDO::getCollegeName, ClassesDO::getCollegeName)
                .selectAs(ProfessionsDO::getHierarchy, ClassesDO::getHierarchy)
                .orderByDesc(ClassesDO::getCreateTime));
    }

    default List<ClassesDO> selectList(ClassesExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ClassesDO>()
                .eqIfPresent(ClassesDO::getClassesNumber, reqVO.getClassesNumber())
                .likeIfPresent(ClassesDO::getClassesName, reqVO.getClassesName())
                .eqIfPresent(ClassesDO::getClassesCode, reqVO.getClassesCode())
                .eqIfPresent(ClassesDO::getClassesGrade, reqVO.getClassesGrade())
                .betweenIfPresent(ClassesDO::getOpeningTime, reqVO.getOpeningTime())
                .orderByDesc(ClassesDO::getCreateTime));
    }

    default List<ClassesDO> selectList0(ClassesExportReqVO reqVO) {
        return selectJoinList(ClassesDO.class, new MPJLambdaWrapperX<ClassesDO>()
                .eqIfPresent(ClassesDO::getClassesNumber, reqVO.getClassesNumber())
                .likeIfPresent(ClassesDO::getClassesName, reqVO.getClassesName())
                .eqIfPresent(ClassesDO::getProfessionId, reqVO.getProfessionsId())
                .eqIfPresent(ClassesDO::getClassesCode, reqVO.getClassesCode())
                .eqIfPresent(ClassesDO::getClassesGrade, reqVO.getClassesGrade())
                .betweenIfPresent(ClassesDO::getOpeningTime, reqVO.getOpeningTime())
                .leftJoin(ProfessionsDO.class, ProfessionsDO::getId, ClassesDO::getProfessionId)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getId, ProfessionsDO::getDepartmentsId)
                .leftJoin(CollegesDO.class, CollegesDO::getId, DepartmentsDO::getCollegeId)
                .selectAll(ClassesDO.class)
                .selectAs(ProfessionsDO::getProfessionName, ClassesDO::getProfessionsName)
                .selectAs(DepartmentsDO::getDepartmentName, ClassesDO::getDepartmentsName)
                .selectAs(CollegesDO::getCollegeName, ClassesDO::getCollegeName)
                .selectAs(ProfessionsDO::getHierarchy, ClassesDO::getHierarchy)
                .orderByDesc(ClassesDO::getCreateTime));
    }

    @DataPermission(enable = false)
    default List<ClassesInfoDO> selectClassBaseInfoByClassesId(Collection<String> classesIds) {
        MPJLambdaWrapper<ClassesDO> classesDOMPJLambdaWrapper = new MPJLambdaWrapperX<ClassesDO>()
                .in(ClassesDO::getId, classesIds)
                .leftJoin(ProfessionsDO.class, ProfessionsDO::getId, ClassesDO::getProfessionId)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getId, ProfessionsDO::getDepartmentsId)
                .leftJoin(CollegesDO.class, CollegesDO::getId, DepartmentsDO::getCollegeId)
                .selectAs(ClassesDO::getId, ClassesInfoDO::getClassesId)
                .selectAs(ProfessionsDO::getId, ClassesInfoDO::getProfessionsId)
                .selectAs(DepartmentsDO::getId, ClassesInfoDO::getDepartmentsId)
                .selectAs(CollegesDO::getId, ClassesInfoDO::getCollegeId)
                .selectAs(ClassesDO::getClassesName, ClassesInfoDO::getClassesName)
                .selectAs(ProfessionsDO::getProfessionName, ClassesInfoDO::getProfessionsName)
                .selectAs(DepartmentsDO::getDepartmentName, ClassesInfoDO::getDepartmentsName)
                .selectAs(ProfessionsDO::getHierarchy, ClassesInfoDO::getHierarchy)
                .selectAs(CollegesDO::getCollegeName, ClassesInfoDO::getCollegeName);

        return selectJoinList(ClassesInfoDO.class, classesDOMPJLambdaWrapper);
    }



    @DataPermission(enable = false)
    default ClassesInfoDO selectClassBaseInfoByClassesIdSelectOne(String classesIds) {
        List<ClassesInfoDO> classesInfoDOS = selectClassBaseInfoByClassesId(Collections.singleton(classesIds));
        if (classesInfoDOS.isEmpty()) {
            return null;
        }
        return classesInfoDOS.stream().findFirst().orElse(null);
    }


}
